package com.guagua.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guagua.blog.entity.Favorite;
import com.guagua.blog.mapper.FavoriteMapper;
import com.guagua.blog.service.FavoriteService;
import org.springframework.stereotype.Service;

@Service
public class FavoriteServiceImpl extends ServiceImpl<FavoriteMapper, Favorite> implements FavoriteService {

    @Override
    public boolean toggleFavorite(Long articleId, Long userId) {
        boolean isFavorited = isFavorited(articleId, userId);
        
        if (isFavorited) {
            // 取消收藏
            return lambdaUpdate()
                .eq(Favorite::getArticleId, articleId)
                .eq(Favorite::getUserId, userId)
                .remove();
        } else {
            // 添加收藏
            Favorite favorite = new Favorite();
            favorite.setArticleId(articleId);
            favorite.setUserId(userId);
            return save(favorite);
        }
    }

    @Override
    public boolean isFavorited(Long articleId, Long userId) {
        return lambdaQuery()
            .eq(Favorite::getArticleId, articleId)
            .eq(Favorite::getUserId, userId)
            .exists();
    }
} 